program main

    use easy_lua_m, only: get_value
    use, intrinsic :: iso_c_binding, only: c_ptr
    use lua
    implicit none

    type(c_ptr) :: l
    integer rc, i
    real, allocatable :: y(:), x(:, :)
    character(:), allocatable :: s
    logical :: b

    inquire (file='example/demo.lua', exist=b)
    if (b) then
        l = lual_newstate()
        call lual_openlibs(l)
        rc = lual_loadfile(l, "example/demo.lua")
        rc = lua_pcall(l, 0, 0, 0) ! 先运行脚本，引入函数
        rc = lua_getglobal(l, "make_array")
        ! 在运行函数前，有传入函数，应该push参数值到Lua虚拟机堆栈。
        rc = lua_pcall(l, 0, 3, 0) ! 运行函数
        call get_value(l, x, index=-3)
        print *, "c(REAL, para1) = ", x
        call get_value(l, y, index=-2) !@tofix: this is not working
        print *, "a(REAL, para2) = ", y
        call get_value(l, y, index=-1) ! index -1 by default, same as `get_value(l, y, index=-1)`
        print *, "b(REAL, para3) = ", y

        call lua_close(l)
    end if

    ! 如果数据量较小的话，可以在Lua中运行函数给变量，传递变量，而不是临时函数返回值。
    ! 当然，大数组和大多数情况，还是直接调用函数比较实用。

end program main

! c(REAL, para1) =    3.00000000       4.00000000       4.00000000       5.00000000       5.00000000       6.00000000       6.00000000       7.00000000       7.00000000       8.00000000       0.00000000       0.00000000
! a(REAL, para2) =    1.00000000       2.00000000       3.00000000       4.00000000       5.00000000
! b(REAL, para3) =    2.00000000       3.00000000       4.00000000       5.00000000       6.00000000
